<?php

class myUser extends sfBasicSecurityUser
{
	/**
	 * login
	 *
	 * @param User $objTUser
	 */
	public function login($objTUser)
	{
		$this->Initialization($objTUser->getId());
	}

	/**
	 * user logout
	 */
	public function logout( $boSessionClear=TRUE )
	{

		$this->setAuthenticated(false);

		$this->clearCredentials();

		if ( $boSessionClear ) {
			$this->getAttributeHolder()->clear();
		}

		sfContext::getInstance()->getResponse()->setCookie('sandbox', '', time() - 3600, '/');

		return TRUE;
	}
	
	public function signIn( $objStaff )
	{
		$this->setAuthenticated(true);
		$this->addCredential("admin");

		// ä¹±æ•°é�µç”Ÿæˆ�
		$aryRandSeed = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",0,1,2,3,4,5,6,7,8,9);
		$strRememberKey = "";
		for( $i=0; $i<32; $i++ ) {
			$key = mt_rand(0,51);
			$strRememberKey .= $aryRandSeed[$key];
		}

		// Cookieã�«ä¹±æ•°é�µã‚’ä¿�å­˜
		$value = base64_encode( serialize( array($strRememberKey,$objStaff->getTUserId()) ) );
		sfContext::getInstance()->getResponse()->setCookie('hybrid', '', time() - 3600, '/');
		sfContext::getInstance()->getResponse()->setCookie('hybrid', $value, time()+60*60*24*7, '/');

		return $strRememberKey;
	}


	/** signOut ********************************************/
	/* visibility	.. Public
	/* return		.. TRUE	.. Boolean
	********************************************************/
	public function signOut()
	{
		$this->logout();
	}

	/**
	 *
	 * @param  integer		$intUserId
	 * @param  integer		$intAdminSiteId
	 */
	public function initUser( $intUserId, $intAdminSiteId = 1 )
	{
		$this->Initialization($intUserId);
	}
	
/**
	 * Initialization
	 *
	 * @return boolean
	 */
	public function Initialization( $intUserId )
	{
		$objUser = UserTablePeer::retrieveByPk($intUserId);
		if ( !$objUser || $objUser->getIsDeleted() == TRUE ) {
			return false;
		}

		$this->logout(FALSE);

		$this->setAuthenticated(true);

		$this->setCulture("vn");

		$this->setAttribute('userId',	 $objUser->getId());
		$this->setAttribute('userName',	 $objUser->getName());
		$this->setAttribute('login_id',	 $objUser->getMailAddress());
		$this->setAttribute('isAdmin',	 $objUser->getIsAdmin());
	
		$aryLoginParameter = array();

		$aryLoginParameter["user_id"] = $objUser->getId();

		$aryLoginParameter["name"] = $objUser->getName();

		$aryLoginParameter["login_id"] = $objUser->getMailAddress();

		$aryLoginParameter["isAdmin"] = $objUser->getIsAdmin();

		$this->setAttribute( "LoginParameter", $aryLoginParameter );

//		$strRememberKey = $this->makeRandomKey();
//		$objUser->setRememberKey($strRememberKey);
//		$objUser->save();

//		if ( sfConfig::get('sf_environment') == "prod" ) {
//			$con = pg_connect("host=192.168.10.100 port=5432 dbname=hybrid user=hybrid password=YpzCcyn1");
//		} else {
//			$con = pg_connect("host=203.143.106.75 port=5432 dbname=hybrid user=hybrid password=knCqItuR");
//		}
//		$strUpdateSql = "UPDATE r_staff_information SET remember_key = '%s', last_login_time = now() WHERE t_user_id = %d";
//		pg_query($con, sprintf($strUpdateSql,$strRememberKey,$objUser->getId()));
//		pg_close($con);

		return true;
	}
	
	public function makeRandomKey()
	{
		// ä¹±æ•°é�µç”Ÿæˆ�
		$aryRandSeed = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",0,1,2,3,4,5,6,7,8,9);
		$strRememberKey = "";
		for( $i=0; $i<32; $i++ ) {
			$key = mt_rand(0,51);
			$strRememberKey .= $aryRandSeed[$key];
		}

		$value = base64_encode( serialize( array($strRememberKey,$this->getTUserId()) ) );
		sfContext::getInstance()->getResponse()->setCookie('hybrid', '', time() - 3600);
		sfContext::getInstance()->getResponse()->setCookie('hybrid', $value, time()+60*60*24*7);

		return $strRememberKey;
	}
}
